package com.learn.training.multithread.chapter6.minchapter8.demo3;

import java.util.concurrent.Semaphore;

public class SemphoreDemo {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(1);
        Thread t1 = new Thread(() -> {
            try {
                semaphore.acquire();
                System.out.println("thread 1 is running");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            finally {
                semaphore.release();
                System.out.println("thread 1 is released");
            }
        });

        Thread t2 = new Thread(() -> {
            try {
                semaphore.acquire();
                System.out.println("thread2 is running");
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            finally {
                semaphore.release();
                System.out.println("thread 2 is released");
            }
        });

        t1.start();
        t2.start();

    }
}
